package com.android.tabdemo;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.app.ListActivity;
import android.app.SearchManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;

public class SearchableActivity extends ListActivity {

	private ListView listView;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		// Get the intent, verify the action and get the query
		//setContentView(R.layout.search_layout);
		//listView = (ListView) findViewById(R.id.list);
		listView = getListView();
		
	    Intent intent = getIntent();
	    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
	      String query = intent.getStringExtra(SearchManager.QUERY);
	      Toast.makeText(this, query, Toast.LENGTH_LONG).show();
	      doMySearch(query);
	    }
	}
	 private void doMySearch(String query) {
//			Bundle bundle = getIntent().getExtras();
//			if(bundle!=null)
		//	{
//				ArrayList<Friend> friends=bundle.getParcelableArrayList("friendlist");
				
//			}
		 ArrayList<Friend> friends = MainActivity.friends;
		 ArrayList<Friend> friend = new ArrayList<Friend>();
		 for (int i = 0; i < friends.size(); i++) {
			 Pattern pattern = Pattern.compile(query);
			 Matcher matcher = pattern.matcher(friends.get(i).name);
			if(matcher.find())
				friend.add(friends.get(i));
		}
		 FriendAdapter fAdapter = new FriendAdapter(getBaseContext(),friend);
		 listView.setAdapter(fAdapter);
	}
	 
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		Friend temp = (Friend)listView.getItemAtPosition(position);
		Intent intent2 = new Intent("com.android.tabdemo.HuihuaActivity");
		intent2.putExtra("talktowho",temp.name);
		startActivity(intent2);
	}
	 
}

	
